package org.nobody.mallb2c.mallseckill.controller;

import lombok.extern.slf4j.Slf4j;
import org.nobody.common.utils.R;
import org.nobody.mallb2c.mallseckill.service.SeckillService;
import org.nobody.mallb2c.mallseckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 3489309323@qq.com
 * @create 2020-09-28-16:44
 */
@Slf4j
@Controller
public class SeckillController {
  @Autowired
  SeckillService seckillService;

  /**
   * 返回当前时间可以参与秒杀的商品信息
   * @return
   */
  @ResponseBody
  @GetMapping("/currentSeckillSkus")
  public R getCurrentSeckillSkus(){
    log.info("currentSeckillSkus正在执行...");
    List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
    return R.ok().setData(vos);
  }

  @ResponseBody
  @GetMapping("/sku/seckill/{skuId}")
  public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
    /**  测试 手动指定降级策略*/
//    try {
//      Thread.sleep(300);
//    } catch (InterruptedException e) {
//      e.printStackTrace();
//    }
    SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
    return R.ok().setData(to);
  }

  @GetMapping("/kill")
  public String secKill(
    @RequestParam("killId") String killId,
    @RequestParam("key") String key,
    @RequestParam("num") Integer num,
    Model model
  ){
    String orderSn = seckillService.kill(killId,key,num);
    System.out.println("secKill orderSn:"+orderSn);
    model.addAttribute("orderSn", orderSn);
    return "success";
  }
}
